<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Category;
use App\Shape;
use App\Data;
class categoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $pageListData=Category::getTree(0,1);
        return view('admin.category.index',compact('pageListData'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $selectData=Category::getTree(0,1);
        $shapeList=Shape::all();
        return view('admin.category.form',compact('selectData','shapeList'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $schema=[
            'category_name'=>'required',
            'pid'=>'required',
            'shape_id'=>'required',
        ];
        $request->validate($schema);
        $createData=$request->only(['category_name','pid','shape_id']);
        Category::create($createData);
        return json(200,'创建成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $selectData=Category::getTree(0,1);
        $shapeList=Shape::all();
        $editData=Category::find($id);
        return view('admin.category.form',compact('editData','selectData','shapeList'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if($request->input('tree')){
            $tree=json_decode($request->input('tree'),true);
            function changeAll($data,$pid=0){
                foreach($data as $item){
                    Category::find($item['id'])->update(['pid'=>$pid]);
                    if(isset($item['children'])){
                        changeAll($item['children'],$item['id']);
                    }
                }
            }
            changeAll($tree);
            return json(200,'修改成功');
        }else{
            $schema=[
                'category_name'=>'required',
                'pid'=>'required',
                'shape_id'=>'required',
            ];
            $request->validate($schema);
            $updateData=$request->only(['category_name','pid','shape_id']);
            Category::find($id)->update($updateData);
            return json(200,'修改成功');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {   
        //是否被使用
        $is_has=Data::where('category_id',$id)->exists();
        if($is_has){
            return json(105,'删除失败,当前分类正在被使用');
        }
        //是否有子菜单
        $is_has=Category::where('pid',$id)->exists();
        if($is_has){
            return json(105,'删除失败,当前分类拥有子分类');
        }
        $res=Category::find($id)->delete();
        if($res) return json(200,'删除成功');
        return json(105,'删除失败');
    }
}
